package com.spzx.product.bitmap;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName: BitmapSyncService
 * Description:
 *
 * @Author linz
 * @Creat 2025/3/19 18:59
 * @Version 1.00
 */
@Component
public class BitmapSyncService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    private static final String BITMAP_KEY = "product:sku:data";



    /**
     * 更新 Bitmap 中的位状态
     * @param id    productSkuID
     * @param exist 是否存在（true=存在，false=不存在）
     */
    public void updateBitmap(Long id, boolean exist) {
        redisTemplate.opsForValue().setBit(BITMAP_KEY, id, exist);
    }

    /**
     * 批量初始化 Bitmap（服务启动时调用）
     */
    @PostConstruct
    public void initBitmap() {
        List<ProductSku> productSkuIdList = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getStatus, 1).eq(ProductSku::getDelFlag, 0).select(ProductSku::getId));
        for (ProductSku productSku : productSkuIdList) {
            Long id = productSku.getId();
            //System.out.println("id = " + id);
            redisTemplate.opsForValue().setBit(BITMAP_KEY,id,true);
        }
    }

}
